package com.data.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.config.dao.ChargeConfigDao;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.dao.ShiftConfigDao;
import com.config.entity.ChargeConfig;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.config.entity.ShiftConfig;
import com.data.dao.BaseDataDao;
import com.data.dao.ChargeMinDao;
import com.data.dao.ChargeShiftDao;
import com.data.entity.ChargeMin;
import com.data.entity.ChargeShift;

@Service("chargeShiftService")
public class ChargeShiftServiceImpl implements ChargeShiftService{
	@Resource(name="baseDataDao")
	private BaseDataDao baseDataDao;
	@Resource(name="lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name="equipmentDao")
	private EquipmentDao equipmentDao;
	@Resource(name="chargeShiftDao")
	private ChargeShiftDao chargeShiftDao;
	@Resource(name="chargeMinDao")
	private ChargeMinDao chargeMinDao;
	@Resource(name="shiftConfigDao")
	private ShiftConfigDao shiftConfigDao;
	@Resource(name="chargeConfigDao")
	private ChargeConfigDao chargeConfigDao;
	/**
	 * 根据时间保存班次电费记录
	 */
	@Override
	public void saveChargeShift(Date belongTime) {
			List<LineConfig> lines=lineConfigDao.findAllLine();
			if(lines.size()!=0){
				for(LineConfig line:lines){
				//	this.saveLineChargeShift(line.getId(), belongTime);  生产线不提取数据
					List<Equipment> equs=equipmentDao.listByLine(line.getId());
					if(equs.size()!=0){
						for(Equipment e:equs){
							this.saveEquChargeShift(e.getId(), line.getId(), belongTime);
						}
					}
				}
			}
  }
	/**
	 * 提取生产线班次电费记录数据
	 * @param lineId
	 * @param shiftId
	 * @param chargeName
	 * @param belongTime
	 */
//	private void saveLineChargeShift(int lineId,Date belongTime){
//		try{
//			SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			SimpleDateFormat sdt = new SimpleDateFormat("HH:mm:ss");
//			List<ChargeConfig>configs=chargeConfigDao.findDistinctChargesName();
//			// 查询班次
//			ShiftConfig config=shiftConfigDao.findShiftConfigByDate(lineId, belongTime);
//			if(configs.size()!=0){
//				for(ChargeConfig charge:configs){
//					if(config!=null){
//						//查询是否已保存班次电记录数据
//						ChargeShift shift=chargeShiftDao.getChargeShiftByIdAndDate(lineId, config.getId(),charge.getChargeName(), sdf.format(belongTime),"Y");
//						boolean isNew=false;
//						if(shift==null){
//							shift=new ChargeShift();
//							shift.setLineId(lineId);
//							shift.setShiftId(config.getId());
//							shift.setShiftName(config.getShiftName());
//	                        shift.setBelongDate(sdf.parse(sdf.format(belongTime)));
//	                        shift.setChargeName(charge.getChargeName());
//							isNew=true;
//						}
//						Calendar ca=Calendar.getInstance();
//						ca.setTime(config.getStartTime());
//						String date=sdf.format(belongTime)+" "+sdt.format(ca.getTime());
//						List<ChargeMin> list=chargeMinDao.getChargeMinsByDate(lineId, date, sdft.format(belongTime),charge.getChargeName(),"Y");
//						double relativeComp=0.0;double charges=0.0;
//						if(list.size()!=0){
//							for(ChargeMin min:list){
//								relativeComp+=min.getRelativeComp();
//								charges+=min.getCharge();
//							}
//						}
//						shift.setRelativeComp(relativeComp);
//						shift.setCharge(charges);
//						if(isNew){
//							chargeShiftDao.save(shift);
//						}else{
//							chargeShiftDao.update(shift);
//						}
//					 }
//				}
//			}		
//		} catch (ParseException e1) {
//				e1.printStackTrace();
//  }
//}
	
	/**
	 * 提取生产线班次电费记录数据
	 * @param equId
	 * @param lineId
	 * @param shiftId
	 * @param chargeName
	 * @param belongTime
	 */
	private void saveEquChargeShift(int equId,int lineId,Date belongTime){
		try{
		    	SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat sdt = new SimpleDateFormat("HH:mm:ss");
				List<ChargeConfig>configs=chargeConfigDao.findDistinctChargesName();
				// 查询班次
				ShiftConfig config=shiftConfigDao.findShiftConfigByDate(lineId, belongTime);
				if(configs.size()!=0){
					for(ChargeConfig charge:configs){
						if(config!=null){
						//查询是否已保存班次电记录数据
						ChargeShift shift=chargeShiftDao.getChargeShiftByIdAndDate(equId, config.getId(),charge.getChargeName(), sdf.format(belongTime),"N");
						boolean isNew=false;
						if(shift==null){
							shift=new ChargeShift();
							shift.setEquId(equId);
							shift.setShiftId(config.getId());
							shift.setShiftName(config.getShiftName());
				            shift.setBelongDate(sdf.parse(sdf.format(belongTime)));
				            shift.setChargeName(charge.getChargeName());
							isNew=true;
						}
						Calendar ca=Calendar.getInstance();
						ca.setTime(config.getStartTime());
						String date=sdf.format(belongTime)+" "+sdt.format(ca.getTime());
						List<ChargeMin> list=chargeMinDao.getChargeMinsByDate(equId, date, sdft.format(belongTime),charge.getChargeName(),"N");
						double relativeComp=0.0;double charges=0.0;
						if(list.size()!=0){
							for(ChargeMin min:list){
								relativeComp+=min.getRelativeComp();
								charges+=min.getCharge();
							}
						}
						shift.setRelativeComp(relativeComp);
						shift.setCharge(charges);
						if(isNew){
							chargeShiftDao.save(shift);
						}else{
							chargeShiftDao.update(shift);
						}
					}
				}
			}
		} catch (ParseException e1) {
				e1.printStackTrace();
		}
	}
}
